﻿using Aspfm.Dal;
using Aspfm.Main;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Aspfm.DataBase
{
    public partial class FrmLock : Form
    {
        IDbHelper idb = null;
        public FrmLock(string connkey)
        {
            InitializeComponent();

            
            idb = DbFactory.GetDB(connkey);
            LoadLock();
        }

        private void LoadLock()
        {
            DataSet ds = null;
            if (idb.GetDbType() == DataSourceType.Oracle)
            {
                ds = GetOracleLock();
            }
            else if (idb.GetDbType() == DataSourceType.MSSql)
            {
                ds = GetMSSqlLock();
            }
            if (ds != null && ds.Tables.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0];
            }
        }

        public DataSet GetOracleLock()
        {
            string sqlstr = @"select a.object_name,b.session_id,c.serial#,c.program,c.PREV_EXEC_START,c.username,c.command,c.machine,c.lockwait
from all_objects a,v$locked_object b,v$session c where a.object_id=b.object_id and c.sid=b.session_id";
            return idb.QuerySql(sqlstr);
        }

        public bool KillOracleLock(string session_id, string serial)
        {
            return idb.ExecSql("alter system kill session '" + session_id + "," + serial + "'");
        }

        public DataSet GetMSSqlLock()
        {
            string sqlstr = @"select   request_session_id   spid,OBJECT_NAME(resource_associated_entity_id) tableName   
from   sys.dm_tran_locks where resource_type='OBJECT'";
            return idb.QuerySql(sqlstr);
        }
        public bool KillMSSqlLock(string spid)
        {
            return idb.ExecSql("KILL "+spid);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (idb != null)
            {
                List<int> rowindexlist = GetSelectRowIdList();
                if (rowindexlist.Count > 0)
                {
                    if (MessageBox.Show("是否确定强制终止所选择的：" + rowindexlist.Count + " 种锁", "强制终止", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        foreach (int index in rowindexlist)
                        {
                            if (idb.GetDbType() == DataSourceType.Oracle)
                            {
                                string session_id = dataGridView1.Rows[index].Cells["session_id"].Value.ToString();
                                string serial = dataGridView1.Rows[index].Cells["serial#"].Value.ToString();
                                KillOracleLock(session_id, serial);
                            }
                            else if (idb.GetDbType() == DataSourceType.MSSql)
                            {
                                string spid = dataGridView1.Rows[index].Cells["spid"].Value.ToString();
                                KillMSSqlLock(spid);
                            }
                        }
                    }
                }
            }
            LoadLock();
        }

        private List<int> GetSelectRowIdList()
        {
            List<int> rowindexlist = new List<int>();
            foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
            {
                if (!rowindexlist.Contains(cell.RowIndex))
                {
                    rowindexlist.Add(cell.RowIndex);
                }
            }
            return rowindexlist;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LoadLock();
        }
    }
}
